/***********************************************************************************
**  
* @copyright (c) 2010-2019, ChengDu Duyer Technology Co., LTD. All Right Reserved.
*
************************************************************************************/
/**
* @file	    mjs_web_client.cpp
* @version     
* @brief      
* @author   duye
* @date	    2017-01-16
* @note 
*
* 1. 2017-01-16 duye Created this file
*/

#include <duye_type.h>
#include <duye_xml.h>
#include <duye_buffer.h>
#include <duye_logger.h>
#include <duye_cfg_mgr.h>
#include <mj_net_protocol.h>
#include <mjs_reqer.h>
#include <mjs_on_action.h>
#include <mjs_mysql.h>
#include <mjs_server.h>
#include <mjs_web_client.h>

static const int8* DUYE_LOG_PREFIX = "mangjings.web_client";

MJSWebClient::MJSWebClient(const int32 sockfd, const std::string& ip, MJSOnActionMap* on_action_map, MJServer* mjserver) 
	: MJSClient(sockfd, ip, on_action_map, mjserver, MJSClient::CLIENT_MJW) {
}

MJSWebClient::~MJSWebClient() {
}

bool MJSWebClient::init() {
	if (!duye::CfgMgr::ins().getValue("node:mjw.param:ip", m_ip, "value")) {
		DUYE_ERROR("get mjw config ip failed");
		return false;
	}

	if (!duye::CfgMgr::ins().getValue("node:mjw.param:username", m_username, "value")) {
		DUYE_ERROR("get mjw config username failed");
		return false;
	}

	if (!duye::CfgMgr::ins().getValue("node:mjw.param:pwdmd5", m_pwdmd5, "value")) {
		DUYE_ERROR("get mjw config pwdmd5 failed");
		return false;
	}

	return true;
}

bool MJSWebClient::handleAction(const std::list<MJMsgAction>& action_list) {
	MJSOnActionMap::iterator act_iter;
	if (authStatus() != AUTH_OK) {
		const MJMsgAction& action = action_list.front();
		if (action.getName() != "web_auth") {
			DUYE_WARN("client:%s don't auth, can't handle others action", m_ip.c_str());
			return false;
		}
	}

	std::list<MJMsgAction>::const_iterator iter = action_list.begin();
	for (; iter != action_list.end(); ++iter) {
		const MJMsgAction& action = *iter;
		MJSOnActionMap::iterator act_iter = m_on_action_map->find(action.getName());
		if (act_iter == m_on_action_map->end()) {
			DUYE_ERROR("have no action:%s in m_on_action_map", action.getName().c_str());
			continue;
		}

		act_iter->second->onAction((MJMsgAction*)&action, m_reqer);
	}

	return true;
}

bool MJSWebClient::innerNotify(const MJSInnerMsgType& msg_type, void* data) {
	switch (msg_type) {
		default:
			DUYE_ERROR("unknown inner message type : %d", msg_type);
			return false;
			break;
	}

	return true;
}